package rexsee.content;

import rexsee.core.application.RexseeApplication;
import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import rexsee.core.browser.clazz.SyncTask;
import rexsee.core.browser.clazz.SyncTask.StringRunnable;
import rexsee.core.utilities.Json;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

public class RexseeContent implements JavascriptInterface {

	public static final String INTERFACE_NAME = "Content";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeContent(childBrowser);
	}

	private final Browser mBrowser;
	private final Context mContext;

	public RexseeContent(Browser browser) {
		mBrowser = browser;
		mContext = mBrowser.getContext();
	}

	public static String getContentColumns_(Context context, Uri contentUri) {
		if (contentUri == null) return "[]";
		try {
			ContentResolver contentResolver = context.getContentResolver();
			Cursor cursor = contentResolver.query(contentUri, null, null, null, null);
			String rtn = Json.toJsonColumns(cursor);
			cursor.close();
			return rtn;
		} catch (Exception e) {
			return RexseeApplication.EXCEPTION_PREFIX + e.getLocalizedMessage();
		}
	}
	public static String getContentData_(Context context, Uri contentUri, String columns, String selection, String sort) {
		if (contentUri == null) return "[]";
		final String[] columns_ = (columns != null) ? columns.split(",") : null;
		try {
			ContentResolver contentResolver = context.getContentResolver();
			Cursor cursor = contentResolver.query(contentUri, columns_, selection, null, sort);
			String rtn = Json.toJson(cursor);
			cursor.close();
			return rtn;
		} catch (Exception e) {
			return RexseeApplication.EXCEPTION_PREFIX + e.getLocalizedMessage();
		}
	}
	public static String getContentData_(final Browser browser, final Uri contentUri, final String columns, final String selection, final String sort, boolean showProgress, boolean cancelable) {
		StringRunnable runnable = new StringRunnable() {
			@Override
			public String run() {
				return getContentData_(browser.getContext(), contentUri, columns, selection, sort);
			}
		};
		if (showProgress) {
			return new SyncTask().run(runnable, browser, cancelable);
		} else {
			return new SyncTask().run(runnable, null, false);
		}
	}

	//JavaScript interface
	public String getContentColumns(String contentUri) {
		return getContentColumns_(mContext, Uri.parse(contentUri));
	}
	public String getContentData(String contentUri, String columns, String selection, String sort, boolean showProgress, boolean cancelable) {
		if (columns.equals("")) columns = null;
		if (selection.equals("")) selection = null;
		if (sort.equals("")) sort = null;
		return getContentData_(mBrowser, Uri.parse(contentUri), columns, selection, sort, showProgress, cancelable);
	}

}
